package com.epoint.mobileoa.personalized.epointcompany.ui;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.ResKey;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.AddChuchaibaoxiaomingxiTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.InitAddWorkContentPageInfoTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.InitBaoxiaomingxileibieTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.InitChuchaijihuaTask;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.ControlsHelp;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
//import com.epoint.mobileoa.tools.controls.tree.NewTree;
//import com.epoint.mobileoa.tools.controls.tree.Tree;
import com.epoint.mobileoa.tools.controls.tree.codeitemtree.CodeItemTree;
import com.epoint.mobileoa.tools.controls.tree.codeitemtree.NewCodeItemTree;
import com.epoint.mobileoa.tools.controls.tree.codeitemtree.TreeDateDealHelp;
import com.epoint.mobileoa.ui.deptchoose.DeptChooseSimpleView;
import com.epoint.mobileoa.ui.personchoose.PersonChooseViewNew;

/**
 * @author liyc
 * @time 2012-4-19 下午7:46:51
 * @annotation 新增费用报销明细
 */
public class AddFeiyongbaoxiaoDetailView extends SuperView {

	private Button btFsrq,btFylb,btTjbc,btFyry,btFsd,btFybm;
	private EditText etFsrq,etFylb,etFybm,etFyry,etFsd,etFyje,etBz,etGllc;
	private int taskid;//1初始化页面数据,2添加保存数据
	private int resumeType;//1费用类别恢复,2人员选择恢复,3发生地,4费用部门

	private String fyryname,fyryguid;

	private RelativeLayout rlFsd,rlFyry;

	private String rzdate;

	public static String rzguid,addresscode,addressname;

	private LinearLayout llGllc;
	@Override
	public void initControls() {
		addresscode = getIntent().getStringExtra("addresscode");
		addressname = getIntent().getStringExtra("addressname");

		btFybm = (Button) findViewById(R.id.btFybm);
		btFybm.setOnClickListener(this);

		etGllc = (EditText) findViewById(R.id.etGllc);

		llGllc = (LinearLayout) findViewById(R.id.llGllc);

		etBz = (EditText) findViewById(R.id.etBz);
		etFyje = (EditText) findViewById(R.id.etFyje);
		etFsd = (EditText) findViewById(R.id.etFsd);
		rlFsd = (RelativeLayout) findViewById(R.id.rlFsd);
		rlFyry = (RelativeLayout) findViewById(R.id.rlFyry);
		btFsd = (Button) findViewById(R.id.btFsd);
		btFsd.setOnClickListener(this);
		btFsrq = (Button) findViewById(R.id.btFsrq);
		btFsrq.setOnClickListener(this);
		btFsrq.setVisibility(View.GONE);
		etFsrq = (EditText) findViewById(R.id.etFsrq);
		btFylb = (Button) findViewById(R.id.btFylb);
		btFylb.setOnClickListener(this);
		btTjbc = (Button) findViewById(R.id.btTjbc);
		btTjbc.setOnClickListener(this);
		btFyry = (Button) findViewById(R.id.btFyry);
		btFyry.setOnClickListener(this);
		etFylb = (EditText) findViewById(R.id.etFylb);
		etFybm = (EditText) findViewById(R.id.etFybm);
		etFyry = (EditText) findViewById(R.id.etFyry);
		etFsd.setText(addressname);
		etFsd.setTag(addresscode);
		//		if(InitAddWorkContentPageInfoTask.getWorkTypeCodeItemsCount(this,"报销明细类别")==0){
		//			top_pb.setVisibility(View.VISIBLE);
		//			taskid = 1;
		//			Task task = new InitBaoxiaomingxileibieTask(this,null);
		//			MainService.addTask(task);
		//		}
		//
		//		if(InitAddWorkContentPageInfoTask.getWorkTypeCodeItemsCount(this,"地区代码")==0){
		//			showDialog_wait(null);
		//			taskid = 1;
		//			Task task = new InitChuchaijihuaTask(this, null);
		//			MainService.addTask(task);
		//		}



		rzdate = getIntent().getStringExtra("rzdate").substring(0, 10);
		etFsrq.setText(rzdate);
		rzguid = getIntent().getStringExtra("rzguid");

		fyryname = fp.getDisplayname();
		fyryguid = fp.getUserguid();
		etFybm.setText(DBHelp.getUserOuname(this, fyryguid));
		etFybm.setTag(DBHelp.getUserOuguid(this, fyryguid));
		etFyry.setText(fyryname);

		setColor();
	}

	/**
	 * 不同主题设置不同文字颜色
	 */
	private void setColor() {
		setTextViewFontColor((TextView)findViewById(R.id.tvFybm));
		setTextViewFontColor((TextView)findViewById(R.id.tvFyry));
		setTextViewFontColor((TextView)findViewById(R.id.tvFsd));
		setTextViewFontColor((TextView)findViewById(R.id.tvBxbz));
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskid==1){

			}
			else if(taskid==2){
				String bs = param[1].toString();
				//<Result>True</Result><RZRowGuid>test</RZRowGuid><AlertString></AlertString>
				if(bs.contains("<Result>True</Result>")){
					String alert = StringHelp.getXMLAtt(bs, "AlertString");
					if(!alert.equals("")){
						ToastHelp.showToast(this,alert);
					}
					else{
						ToastHelp.showToast(this,"报销明细保存成功");
					}
					finish();
					rzguid = StringHelp.getXMLAtt(bs, "RZRowGuid");
				}
				else{
					ToastHelp.showToast(this, "保存失败");
				}
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
		if(m_Dialog!=null){
			m_Dialog.dismiss();	
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.epoint_addfeiyongbaoxiaoview;
	}

	@Override
	public String setTitle() {
		return "出差报销明细";
	}

	@Override
	public void onClick(View v) {
		if(v==btFsrq){
			AlertHelp.showDatePickDialog(this, new OnDateSetListener() {

				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					etFsrq.setText(year + "-" + DateHelp.switchDay((monthOfYear+1)) + "-" + DateHelp.switchDay(dayOfMonth));
				}
			},null);
		}
		else if(v==btFylb){
			resumeType= 1;
			//			new NewTree(this, "出差报销明细类别", "出差报销明细类别", 2,"",null,true,1);

			new NewCodeItemTree(this, ResKey.ccbxmxtreenode, "报销明细类别", "报销明细类别", 2, "", null, true, 1,"Epoint_InitBaoxiaomingxileibie");


		}
		else if(v==btTjbc){
			if(ControlsHelp.Check_EditText_Blank_Warning(etFylb, "请添加费用类别")){
				return;
			}

			if(ControlsHelp.Check_EditText_Blank_Warning(etFsrq, "请添加发生日期")){
				return;
			}

			if(ControlsHelp.Check_EditText_Blank_Warning(etFyje, "请输入费用金额")){
				return;
			}

			if(rlFsd.getVisibility()==View.VISIBLE){
				if(ControlsHelp.Check_EditText_Blank_Warning(etFsd, "请添加发生地点")){
					return;
				}
			}

			if(rlFyry.getVisibility()==View.VISIBLE){
				if(ControlsHelp.Check_EditText_Blank_Warning(etFyry, "请添加费用人员")){
					return;
				}
				if(etFyry.getText().toString().split(";").length>1)
				{
					etFyry.setError("费用人员只能选择一个人");
					etFyry.requestFocus();
					return;
				}
			}

			taskid = 2;
			showDialog_wait("正在保存...");

			if(rlFsd.getVisibility()==View.GONE){
				etFsd.setText("");
				etFsd.setTag("");
			}

			if(rlFyry.getVisibility()==View.GONE){
				etFyry.setText("");
				etFyry.setTag("");
			}

			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("RZRowGuid", rzguid);
			param.put("RZDate", rzdate);

			if(rlFyry.getVisibility()==View.VISIBLE){
				param.put("UserGuid",fyryguid);
				param.put("UserName", fyryname);
			}
			else{
				param.put("UserGuid","");
				param.put("UserName", "");	
			}

			param.put("BXMoney", etFyje.getText().toString());

			if(rlFsd.getVisibility()==View.VISIBLE){
				param.put("FaShengDi",StringHelp.cutLastSymbol(etFsd.getTag()==null?"":etFsd.getTag().toString(), ";"));
			}
			else{
				param.put("FaShengDi", "");
			}

			param.put("BXType", StringHelp.cutLastSymbol(etFylb.getTag().toString(), ";"));
			param.put("BXTime", rzdate);
			param.put("Remarks", etBz.getText().toString());
			param.put("ZhaoDaiDX", "");
			param.put("XiangMuMC", "");
			param.put("ProjectGuid", "");

			if(llGllc.getVisibility()==View.VISIBLE){
				param.put("KMnum", etGllc.getText().toString());
			}
			else{
				param.put("KMnum","");
			}

			param.put("KeHuDQ", "");
			param.put("OuName", etFybm.getText().toString());
			param.put("OuGuid", etFybm.getTag().toString());

			Task task = new AddChuchaibaoxiaomingxiTask(this, param);
			MainService.addTask(task);
		}
		else if(v==btFyry){
			resumeType = 2;
			PersonChooseViewNew.clist = StringHelp.String2List(fyryname, fyryguid);
			Intent it = new Intent(this,PersonChooseViewNew.class);
			startActivity(it);
		}
		else if(v==btFsd){
			resumeType = 3;
			//			new NewTree(this, "地区选择", "地区代码", 2, "",null,false,3);
			new NewCodeItemTree(this, ResKey.dqdmtreenode, "地区选择", "地区代码", 2, "", null, false, 3,"GetCode_ItemTreeData");
		}
		else if(v==btFybm){
			resumeType = 4;
			Intent intent = new Intent(this,DeptChooseSimpleView.class);
			startActivity(intent);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if(resumeType==1){

			/*List<Map<String,String>> chooseNodeData = CodeItemTree.chooseNodeData;
			if(chooseNodeData!=null){
				if(chooseNodeData.size()>0){
					String ccdq = "";
					String ccdqcode = "";
					for(int i=0;i<chooseNodeData.size();i++){
						ccdq += chooseNodeData.get(i).get("text")+";";
						ccdqcode += chooseNodeData.get(i).get("param1")+";";
					}

					etFsd.setText(ccdq);
					etFsd.setTag(ccdqcode);
				}
			}*/
			TreeDateDealHelp.DealChooseTreeData(etFylb);

			String tagcode = etFylb.getTag().toString();

			if(tagcode.indexOf("01")==0){
				rlFsd.setVisibility(View.VISIBLE);
				rlFyry.setVisibility(View.VISIBLE);

				if(tagcode.equals("010401")){
					llGllc.setVisibility(View.VISIBLE);
				}
				else{
					llGllc.setVisibility(View.GONE);
				}

				if(tagcode.equals("010301")||tagcode.equals("010302")){
					rlFsd.setVisibility(View.GONE);
				}
				else{
					rlFsd.setVisibility(View.VISIBLE);
				}
			}

			else if(tagcode.indexOf("02")==0){
				rlFsd.setVisibility(View.GONE);
				rlFyry.setVisibility(View.GONE);
				llGllc.setVisibility(View.GONE);
			}

			else if(tagcode.indexOf("03")==0){
				rlFsd.setVisibility(View.VISIBLE);
				rlFyry.setVisibility(View.GONE);
				llGllc.setVisibility(View.GONE);
			}

			else if(tagcode.indexOf("04")==0){
				rlFsd.setVisibility(View.GONE);
				rlFyry.setVisibility(View.VISIBLE);
				llGllc.setVisibility(View.GONE);
			}

			else if(tagcode.indexOf("05")==0){
				rlFsd.setVisibility(View.GONE);
				rlFyry.setVisibility(View.GONE);
				llGllc.setVisibility(View.GONE);
			}

			else if(tagcode.indexOf("10")==0){//公司派车
				rlFsd.setVisibility(View.GONE);
				rlFyry.setVisibility(View.VISIBLE);
				llGllc.setVisibility(View.GONE);
			}
		}
		else if(resumeType==2){
			String[] info = StringHelp.dealChooseListToString(PersonChooseViewNew.clist);
			fyryname = info[0];
			fyryguid = info[1];
			etFyry.setText(fyryname);
		}
		else if(resumeType==3){
			//			List<Map<String,String>> chooseNodeData = CodeItemTree.chooseNodeData;
			//			if(chooseNodeData!=null){
			//				if(chooseNodeData.size()>0){
			//					String ccdq = "";
			//					String ccdqcode = "";
			//					for(int i=0;i<chooseNodeData.size();i++){
			//						ccdq += chooseNodeData.get(i).get("text")+";";
			//						ccdqcode += chooseNodeData.get(i).get("param1")+";";
			//					}
			//					chooseNodeData.clear();
			//					
			//					etFsd.setText(ccdq);
			//					etFsd.setTag(ccdqcode);
			//				}
			//			}

			TreeDateDealHelp.DealChooseTreeData(etFsd);


			/*

			if(Tree.chooseNodeData!=null){
				if(Tree.chooseNodeData.size()>0){
					String fsd = "";
					String fsdcode = "";

					for(int i=0;i<Tree.chooseNodeData.size();i++){
						fsd += Tree.chooseNodeData.get(i).get("text")+";";
						fsdcode = Tree.chooseNodeData.get(i).get("param1")+";";
					}

					etFsd.setText(fsd);
					etFsd.setTag(fsdcode);
				}
			}

			 */
		}
		else if(resumeType==4){
			if(DeptChooseSimpleView.selectedUO!=null){
				etFybm.setText(DeptChooseSimpleView.selectedUO.ouname);
				etFybm.setTag(DeptChooseSimpleView.selectedUO.ouguid);
			}
		}
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		DeptChooseSimpleView.selectedUO = null;
		CodeItemTree.chooseNodeData.clear();
	}
	
	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}
}
